package com.antdesignbackend.backend.util;

import java.util.Arrays;

public class LatLonUtil {

    private static final double PI = Math.PI;    //老祖真理
    private static final double EARTH_RADIUS = 6378137;    //赤道半径
    private static final double RAD = Math.PI / 180.0;

    /**
     * 根据一点坐标 + 半径 获得相距最近和最远点的坐标
     * @param raidus 单位米
     * return minLat,minLng,maxLat,maxLng
     */
    public static double[][] getAround(double lon,double lat,double raidus){

        double degree = (24901*1609)/360.0;

        double dpmLat = 1/degree;
        double radiusLat = dpmLat*raidus;
        double minLat = lat - radiusLat;
        double maxLat = lat + radiusLat;

        double mpdLng = degree*Math.cos(lat * (PI/180));
        double dpmLng = 1 / mpdLng;
        double radiusLng = dpmLng*raidus;
        double minLng = lon - radiusLng;
        double maxLng = lon + radiusLng;
        return new double[][]{{minLng,minLat},{maxLng,maxLat}};
    }

    /**
     * 根据两点间经纬度坐标（double值），计算两点间距离，单位为米
     * @param lng1
     * @param lat1
     * @param lng2
     * @param lat2
     * @return
     */
    public static double getDistance(double lng1, double lat1, double lng2, double lat2)
    {
        double radLat1 = lat1*RAD;
        double radLat2 = lat2*RAD;
        double a = radLat1 - radLat2;
        double b = (lng1 - lng2)*RAD;
        double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a/2),2) +
                Math.cos(radLat1)*Math.cos(radLat2)*Math.pow(Math.sin(b/2),2)));
        s = s * EARTH_RADIUS;
        s = Math.round(s * 10000) / 10000;
        return s;
    }

    public static void main(String[] args){
        double lat1 = 34.264648;
        double lon1 = 108.952736;

        int radius = 1000;
        //[34.25566276027792,108.94186385411045,34.27363323972208,108.96360814588955]
        double[][] a = getAround(lon1,lat1,radius);
        System.out.println(a[0][0]);

        //911717.0   34.264648,108.952736,39.904549,116.407288
        double dis = getDistance(108.952736,34.264648,116.407288,39.904549);
        System.out.println(dis);
    }
}
